<?php

class Common_model extends CI_Model {

    function __construct() {
        parent::__construct();
        $this->_config = $this->config->item("base");
        $this->_base = $this->config->item("base");
        $this->_upfile = $this->config->item("upfile");
    }

    /*
     * 状态 
     */

    public function getStatus() {
        $outArr = array(
            1 => array("id" => 1, "name" => "是", "tag" => "√"),
            0 => array("id" => 0, "name" => "否", "tag" => "-")
        );
        return $outArr;
    }

    public function yesNo() {
        $outArr = array(
            1 => array("id" => 1, "name" => "是", "tag" => "√"),
            0 => array("id" => 0, "name" => "否", "tag" => "-")
        );
        return $outArr;
    }

    /*
     * 加密密码
     */

    public function encryptPassword($str) {
        $str = md5("password" . $str);
        return $str;
    }

    /*
     * 加密验证码
     */

    public function encryptCaptcha($str) {
        $str = md5("captcha" . $str);
        return $str;
    }

    /*
     * 加密TOKEN
     */

    public function encryptToken($str) {
        $str = md5("token" . $str);
        return $str;
    }

    //发送邮件SMTP
    public function sendMail($paramArr = array()) {

        $optionArr = array(
            "fromName" => "", //发邮件的名称
            "subject" => "", //主题
            "mailTo" => "", //发送对向的邮件地址
            "body" => "", //内容
            "isFile" => 0,
            "fileUrl" => "",
            "addAttachment" => "",
        );
        if (!empty($paramArr)) {
            $optionArr = array_merge($optionArr, $paramArr);
        }
        extract($optionArr);

        $this->load->library('phpmailer');
        $mail = new Phpmailer();
        if ($isFile == 1) {
            $body = file_get_contents($fileUrl);
        }

        $mail->IsSMTP();
        $mail->SMTPAuth = true;

        $mail->FromName = $fromName;
        $mail->Subject = $subject;

        //$body = preg_replace("/\/",'',$body);
        $mail->MsgHTML($body);

        $mail->AddAddress($mailTo, "");

        return $mail->Send();
    }

    function sendJmail($paramArr = array()) {

        $optionArr = array(
            "fromName" => "", //发邮件的名称
            "subject" => "", //主题
            "mailTo" => "", //发送对向的邮件地址
            "body" => "", //内容
            "isFile" => 0,
            "fileUrl" => "",
            "addAttachment" => "",
        );
        if (!empty($paramArr)) {
            $optionArr = array_merge($optionArr, $paramArr);
        }
        extract($optionArr);

        $jmail = new COM('Jmail.Message') or die('无法调用Jmail组件');
        //$jmail=new JMail();
        $jmail->silent = true; //屏蔽例外错误
        $jmail->charset = 'utf8'; //否则中文会乱码
        $jmail->ContentType = "text/html";
        $jmail->From = 'e_noreply@163.com'; //这里理论上是可以不与发件人地址相同的，但用163的试了几次都不行，改成一样的就可以了
        $jmail->FromName = $fromName;
        $jmail->AddRecipient(mailTo); //可添加多个邮件接受者
        $jmail->Subject = $subject;
        //$body = eregi_replace("[\]",'',$body);
        $jmail->Body = $body;
        $jmail->MailServerUserName = 'e_noreply@163.com'; //发信邮件账号
        $jmail->MailServerPassword = 'a123456'; //账户的密码
        try {
            $email = $jmail->Send('smtp.163.com');
            if ($email)
                return true;
            else
                return false;
        } catch (Exception $e) {
            echo $e->getMessage();
        }
    }

    /*
     * 
     */

    public function checkField($param, $type = 1, $transParam = []) {
        $arr = [];
        foreach ($param as $k => $v) {
            $field = $v["field"];
            if ($v["type"] == "int" || $v["type"] == "z_int") {
                if (!empty($transParam)) {
                    ${$field} = isset($transParam[$field]) ? intval($transParam[$field]) : 0;
                } else {
                    ${$field} = $this->input->postInt($field, TRUE);
                }
            } else if ($v["type"] == "float" || $v["type"] == "z_float") {
                if (!empty($transParam)) {
                    ${$field} = isset($transParam[$field]) ? floatval($transParam[$field]) : 0;
                } else {
                    ${$field} = $this->input->postFloat($field, TRUE);
                }
            } else if ($v["type"] == "json") {
                if (!empty($transParam)) {
                    ${$field} = isset($transParam[$field]) ? $transParam[$field] : "";
                } else {
                    ${$field} = $this->input->postStr($field, TRUE, false);
                }
            } else {
                if (!empty($transParam)) {
                    ${$field} = isset($transParam[$field]) ? addslashes(htmlspecialchars($transParam[$field])) : "";
                } else {
                    ${$field} = $this->input->postStr($field, TRUE);
                }
            }
            if ($v["type"] == "string") {
                if ($v["is_need"] == 1) {
                    if (${$field} == "") {
                        throw new Exception(($type == 1 ? "请填上" : "缺少") . $v["name"]);
                    }
                }
            }
            if ($v["type"] == "z_int") {//正整数
                if ($v["is_need"] == 1) {
                    if (${$field} <= 0) {
                        if (isset($v["form_type"]) && $v["form_type"] == "select") {
                            throw new Exception("请选择" . $v["name"]);
                        } else {
                            throw new Exception($v["name"] . "数据有误");
                        }
                    }
                }
            }
            if ($v["type"] == "z_float") {
                if ($v["is_need"] == 1) {
                    if (${$field} <= 0) {
                        throw new Exception($v["name"] . "数据有误");
                    }
                }
            }
            if ($v["type"] == "idcard") {
                if ($v["is_need"] == 1 || ${$field} != "") {
                    if (!isIdCard(${$field})) {
                        throw new Exception($v["name"] . "有误");
                    }
                }
            }
            if ($v["type"] == "date_time") {
                if ($v["is_need"] == 1 || ${$field} != "") {
                    if (strtotime(${$field}) <= 0) {
                        throw new Exception($v["name"] . "格式有误");
                    }
                }
            }
            if ($v["type"] == "mobile") {
                if ($v["is_need"] == 1 || ${$field} != "") {
                    if (!isMobile(${$field})) {
                        throw new Exception($v["name"] . "有误");
                    }
                }
            }
            if ($v["type"] == "json") {
                if ($v["is_need"] == 1 || ${$field} != "") {
                    $array = jsonDecode(${$field});
                    if (!is_array($array) || empty($array)) {
                        throw new Exception($v["name"] . "格式有误");
                    }
                }
            }
            $arr[$field] = ${$field};
        }
        return $arr;
    }

    /*
     * 
     */

    public function checkCompareField($param, $compareParam, $type = 1) {

        $arr = [];
        foreach ($param as $k => $v) {
            $field = $v["field"];
            if ($v["type"] == "int" || $v["type"] == "z_int") {
                ${$field} = !empty($compareParam[$field]) ? intval($compareParam[$field]) : 0;
            } else if ($v["type"] == "float" || $v["type"] == "z_float") {
                ${$field} = !empty($compareParam[$field]) ? floatval($compareParam[$field]) : 0;
            } else {
                ${$field} = !empty($compareParam[$field]) ? addslashes(htmlspecialchars($compareParam[$field])) : "";
            }
            if ($v["type"] == "string") {
                if ($v["is_need"] == 1) {
                    if (${$field} == "") {
                        throw new Exception(($type == 1 ? "请填上" : "缺少") . $v["name"]);
                    }
                }
            }
            if ($v["type"] == "z_int") {//正整数
                if ($v["is_need"] == 1) {
                    if (${$field} <= 0) {
                        throw new Exception($v["name"] . "数据有误");
                    }
                }
            }
            if ($v["type"] == "z_float") {
                if ($v["is_need"] == 1) {
                    if (${$field} <= 0) {
                        throw new Exception($v["name"] . "数据有误");
                    }
                }
            }
            if ($v["type"] == "idcard") {
                if ($v["is_need"] == 1 || ${$field} != "") {
                    if (!isIdCard(${$field})) {
                        throw new Exception($v["name"] . "有误");
                    }
                }
            }
            $arr[$field] = ${$field};
        }
        return $arr;
    }

}
